#region Copyright Notice
/*
Copyright (C) 2011 by Ashley Ross

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion Copyright Notice

using System;
using System.Runtime.InteropServices;

namespace Manta.IO {
    internal static class NativeMethods {
        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)] 
        internal static extern bool CloseHandle(IntPtr objectHandle);

        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        internal static extern IntPtr CreateFile(string fileName, AccessRights desiredAccess, FileShare shareMode, IntPtr securityAttributes, CreationDisposition creationDisposition, FileFlags flagsAndAttributes, IntPtr templateFile);

        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)] 
        internal static extern bool DeviceIoControl(IntPtr device, IOControlCode ioControlCode, IntPtr inBuffer, int inBufferSize, IntPtr outBuffer, int outBufferSize, ref uint bytesReturned, IntPtr overlapped);

        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        [return: MarshalAs(UnmanagedType.Bool)] 
        internal static extern bool CreateHardLink(string fileName, string existingFileName, IntPtr securityAttributes);

        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        [return: MarshalAs(UnmanagedType.U1)]
        internal static extern bool CreateSymbolicLink(string symlinkFileName, string targetFileName, SymbolicLinkFlag flags);
    }

    [Flags]
    internal enum AccessRights : uint {
        // Standard access rights
        Delete = 0x00010000,
        ReadControl = 0x00020000,
        Synchronize = 0x00100000,
        WriteDAC = 0x00040000,
        WriteOwner = 0x00080000,

        StandardRightsAll = Delete | ReadControl | WriteDAC | WriteOwner | Synchronize,
        StandardRightsExecute = ReadControl,
        StandardRightsRead = ReadControl,
        StandardRightsRequired = Delete | ReadControl | WriteDAC | WriteOwner,
        StandardRightsWrite = ReadControl,

        // Access rights that are specific to files and directories
        FileAddFile = 0x0002,                                           // Directory
        FileAddSubdirectory = 0x0004,                                   // Directory
        FileAllAccess = StandardRightsRequired | Synchronize | 0x1FF,   // All
        FileAppendData = 0x0004,                                        // File
        FileCreatePipeInstance = 0x0004,                                // Named Pipe
        FileDeleteChild = 0x0040,                                       // Directory
        FileExecute = 0x0020,                                           // File
        FileListDirectory = 0x0001,                                     // Directory
        FileReadAttributes = 0x0080,                                    // All
        FileReadData = 0x0001,                                          // File & Pipe
        FileReadExtendedAttributes = 0x0008,                            // File & Directory
        FileTraverse = 0x0020,                                          // Directory
        FileWriteAttributes = 0x0100,                                   // all
        FileWriteData = 0x0002,                                         // File & Pipe
        FileWriteExtendedAttributes = 0x0010,                           // File & Directory

        // Generic access rights for files and directories
        GenericRead = 0x80000000,
        GenericWrite = 0x40000000,
        GenericExecute = 0x20000000,
        GenericAll = 0x10000000
    }

    internal enum CreationDisposition : uint {
        CreateNew = 1,
        CreateAlways = 2,
        OpenExisting = 3,
        OpenAlways = 4,
        TruncateExisting = 5
    }

    [Flags]
    internal enum FileFlags : uint {
        BackupSemantics = 0x02000000,
        DeleteOnClose = 0x04000000,
        NoBuffering = 0x20000000,
        OpenNoRecall = 0x00100000,
        OpenReparsePoint = 0x00200000,
        Overlapped = 0x40000000,
        PosixSemantics = 0x01000000,
        RandomAccess = 0x10000000,
        SequentialScan = 0x08000000,
        WriteThrough = 0x80000000,
        FirstPipeInstance = 0x00080000
    }

    [Flags]
    internal enum FileShare : uint {
        None = 0,
        Read = 1,
        Write = 2,
        Delete = 4
    }

    internal enum IOControlCode : uint {
        DeleteReparsePoint = 0x900AC,
        GetReparsePoint = 0x900A8,
        SetReparsePoint = 0x900A4
    }

    internal enum SymbolicLinkFlag : uint {
        File = 0,
        Directory = 1
    }
}